<?php
include_once dirname ( dirname ( __FILE__ ) ) . '/filtro_usuario.php';
include_once dirname ( dirname ( __FILE__ ) ) . '/funciones/basicas.php';
?>
<html>
<head>
<title>buscador facturacion</title>
<?php include_once dirname(dirname(__FILE__)) . '/header.php'?>
<script language="JavaScript">

function addProducto(precio,codigo,nombre,costo)
{
 
   window.opener.document.formulario.codigoProducto.value=codigo;
   //close();
}

function addCliente(rut,codigo,cliente,nomVendedor,codVendedor)
{

  window.opener.document.formulario.rut.value=rut;
  //close();
}
</script>
</head>
<body>

	<form action="buscadorNotaVenta.php" method="post">
		<select name="tipo" size="1">
			<option selected="selected">Que desea buscar?</option>
			<option value="cliente">Cliente</option>
			<option value="producto">Producto</option>
		</select> <br> Buscar:<input type="text" name="buscado"
			value="<?php echo $_POST['buscado'] ?>"> <input type="submit"
			name="buscar" value="buscar">
	</form>

<?php
$conexion = conectar ();
if (isset ( $_POST ['tipo'], $_POST ['buscado'] )) {
	$tipo = $_POST ['tipo'];
	$buscado = $_POST ['buscado'];
}

/* si se busca un producto */
if (isset ( $_POST ['buscar'], $tipo ) && $tipo == 'producto') {
	
	$sql = "SELECT * FROM producto WHERE (cod_prod like'%$buscado%' OR nombre like'%$buscado%') AND estado!='b' ORDER BY nombre";
	$consulta = mysql_query ( $sql, $conexion ) or die ( "Error" );
	$nfilas = mysql_num_rows ( $consulta );
	
	if ($nfilas > 0) {
		
		echo "<table cellpadding=5 cellspacing=0 bgcolor=#000000>
	          <tr>
		      <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Codigo</font></td>
		      <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Nombre</font></td>
	          <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Precio de lista</font></td>
		      <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Stock</font></td>
              <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Acciones</font></td>
		      </tr>";
		
		for($i = 0; $i < $nfilas; $i ++) {
			if ($i % 2 == 0)
				$color = "#CCCC99";
			else
				$color = "#ffffff";
			
			$fila = mysql_fetch_array ( $consulta );
			$costo = $fila ["costo"];
			$codigo = $fila ["cod_prod"];
			$nombre = $fila ["nombre"];
			$precio = $fila ["precio"];
			echo "<tr>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ['cod_prod'] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ["nombre"] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . number_format ( $fila ['precio'], 0, ",", "." ) . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ["cantidad"] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			?>
         <a
		href="javascript:addProducto('<?php echo $precio; ?>','<?php echo $codigo; ?>','<?php echo $nombre; ?>','<?php echo $costo; ?>' );">
		<img title="Seleccionar producto"
		src="<?php echo $_SESSION['BASE'] . '/img/add.png' ?>" border="0">
	</a>
	<a href="javascript: void(0)"
		onClick="javascript:show('<?php echo $_SESSION['BASE'] . "/mantenedor_productos/detalle_producto.php?codigo=" . $codigo . "&viene=1" ?>', 600, 600, 1);">
		<img title="Ver producto"
		src="<?php echo $_SESSION['BASE'] . '/img/search.png' ?>" border="0">
	</a>
	      <?php
			echo "</td>";
			echo "</tr>";
		}
		echo "</table>";
	} else {
		echo "no se encontro resultados";
	}
} /* si se busca un cliente */
else if (isset ( $_POST ['buscar'], $tipo ) && $tipo == 'cliente') {
	$vendedor = $_SESSION ['usuario'];
	
	if (strcasecmp ( $_SESSION ['perfil'], 'user' ) == 0) {
		$sql = "SELECT cliente.* , vendedor.nombre as nomVendedor FROM cliente,vendedor
			   	WHERE (cliente.cod_cli like'%$buscado%' OR cliente.nombre like'%$buscado%')
			   	AND cliente.estado != 'b'
			   	AND cliente.cod_vendedor = '$vendedor'
			   	AND vendedor.cod_vendedor=cliente.cod_vendedor
			   	ORDER BY cliente.nombre";
	} else {
		$sql = "SELECT cliente.* , vendedor.nombre as nomVendedor FROM cliente,vendedor
		         WHERE (cliente.cod_cli like'%$buscado%' OR cliente.nombre like'%$buscado%')
		         AND cliente.estado != 'b'
		         AND vendedor.cod_vendedor=cliente.cod_vendedor
		         ORDER BY cliente.nombre";
	}
	
	$consulta2 = mysql_query ( $sql, $conexion ) or die ( "Error" );
	$nfilas = mysql_num_rows ( $consulta2 );
	
	if ($nfilas > 0) {
		echo "<table cellpadding=5 cellspacing=0 bgcolor=#000000>
		    <tr>
		    <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Codigo</font></td>
		    <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Nombre</font></td>
           <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Rut</font></td>
		    <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Contacto</font></td>
            <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Vendedor</font></td>
            <td bgcolor=#000066><font color=#ffffff face=Arial, Helvetica, sans-serif>Acciones</font></td>
		    </tr>";
		
		for($i = 0; $i < $nfilas; $i ++) {
			if ($i % 2 == 0)
				$color = "#CCCC99";
			else
				$color = "#ffffff";
			
			$fila = mysql_fetch_array ( $consulta2 );
			$rut = $fila ["rut"];
			$codigo = $fila ["cod_cli"];
			$cliente = $fila ["nombre"];
			$codVendedor = $fila ['cod_vendedor'];
			echo "<tr>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ['cod_cli'] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ["nombre"] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ["rut"] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ["contacto"] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			echo "<font color=#000000 face=Arial, Helvetica, sans-serif size=1.5>" . $fila ["nomVendedor"] . "</font>";
			echo "</td>";
			echo "<td bgcolor=" . $color . " class=Estilo2>";
			?>
         <a
		href="javascript:addCliente('<?php echo $rut; ?>','<?php echo $codigo; ?>','<?php echo $cliente; ?>','<?php echo $fila['nomVendedor']; ?>','<?php echo $codVendedor; ?>'); ">
		<img title="Seleccionar cliente"
		src="<?php echo $_SESSION['BASE'] . '/img/add.png' ?>" border="0">
	</a>
	     <?php
			echo "</td>";
			echo "</tr>";
		}
		echo "</table>";
	} else {
		echo "no se encontro resultados";
	}
}
mysql_close ( $conexion );
?>
</body>
</html>
